﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Hotfix
{
    /// <summary>
    /// 
    /// </summary>
    public class XHotfixAssetBundleDataSourceProviderUtility 
    {
        /// <summary>
        /// 
        /// </summary>
        private static List<Type> customNodes;

        /// <summary>
        /// 
        /// </summary>
        public static List<Type> CustomABDataSourceTypes
        {
            get {
                if(customNodes == null)
                {
                    customNodes = BuildCustomAssetBundleDataSourceList();
                }

                return customNodes;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static List<Type> BuildCustomAssetBundleDataSourceList()
        {
            var properList = new List<Type>();
            properList.Add(null);

            var x = AppDomain.CurrentDomain.GetAssemblies();
            foreach (var assembly in x)
            {
                try
                {
                    var list = new List<Type>(assembly.GetTypes().Where(t => t != typeof(XHotfixAssetBundleDataSource))
                        .Where(t => typeof(XHotfixAssetBundleDataSource).IsAssignableFrom(t)));

                    for (int count = 0; count < list.Count; count++)
                    {
                        if (list[count].Name == typeof(XHotfixAssetDatabaseDataSource).Name)
                            properList[0] = list[count];
                        else if (list[count] != null)
                            properList.Add(list[count]);
                    }
                }
                catch (System.Exception e)
                {
                    UnityEngine.Debug.LogException(e);
                }
            }

            return properList;
        }
    }
}
